<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta http-equiv="X-UA-Compatible" content="ie=edge" />
    <title>学生就业统计表</title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }

        a {
            text-decoration: none;
            color: #721c24;
        }

        h1 {
            text-align: center;
            color: #333;
            margin: 20px 0;

        }

        .title {
            width: 933px;
            height: 50px;
            line-height: 50px;
            padding-right: 15px;
            border: 1px solid #ebebeb;
            margin: 10px auto;
            background-color: #f2f2f2;
            text-align: right;
        }

        .title span {
            display: inline-block;
            vertical-align: middle;
            height: 20px;
            margin: -3px 5px 0;
            text-align: center;
            line-height: 20px;
            color: #f26934;
            font-weight: 700;
        }

        table {
            margin: 0 auto;
            width: 950px;
            border-collapse: collapse;
            color: #3c3637;
        }

        th {
            padding: 10px;
            background: #f2f2f2;
            font-size: 18px;
            text-align: left;
        }

        td,
        th {
            border: 1px solid #ebebeb;
            padding: 15px;
        }

        td {

            color: #666;
            font-size: 16px;

        }

        tbody tr {
            background: #fff;
        }

        tbody tr:hover {
            background: #fbfafa;
        }

        tbody a {
            display: inline-block;
            width: 80px;
            height: 30px;
            text-align: center;
            line-height: 30px;
            color: #fff;
            background-color: #f26934;
        }

        .info {
            width: 900px;
            margin: 50px auto;
            text-align: center;
        }

        .info input,
        .info select {
            width: 100px;
            height: 30px;
            outline: none;
            border: 1px solid #ebebeb;
            padding-left: 5px;
            box-sizing: border-box;
            margin-right: 10px;
        }

        .info button {
            width: 70px;
            height: 30px;
            background-color: #5dbfd8;
            outline: none;
            border: 0;
            color: #fff;
            cursor: pointer;
            font-size: 14px;
        }

        .info button:hover {
            background-color: #52abc1;
        }
    </style>
</head>

<body>
    <h1>学生就业统计表</h1>
    <form class="info" autocomplete="off">
        <input type="text" class="uname" name="uname" placeholder="姓名" />
        <input type="text" class="age" name="age" placeholder="年龄" />
        <input type="text" class="salary" name="salary" placeholder="薪资" />
        <select name="gender" class="gender">
            <option value="男">男</option>
            <option value="女">女</option>
        </select>
        <select name="city" class="city">
            <option value="北京">北京</option>
            <option value="上海">上海</option>
            <option value="广州">广州</option>
            <option value="深圳">深圳</option>
            <option value="曹县">曹县</option>
        </select>
        <button class="add">
            <i class="iconfont icon-tianjia"></i>添加
        </button>
    </form>

    <div class="title">共有数据<span>0</span>条</div>
    <table>
        <thead>
            <tr>
                <th>ID</th>
                <th>姓名</th>
                <th>年龄</th>
                <th>性别</th>
                <th>薪资</th>
                <th>就业城市</th>
                <th>录入时间</th>
                <th>操作</th>
            </tr>
        </thead>
        <tbody>
        </tbody>
    </table>
    <script>
        // 参考数据
        const initData = [
            {
                stuId: 1,
                uname: '迪丽热巴',
                age: 22,
                salary: '12000',
                gender: '女',
                city: '北京',
                time: '2099/9/9 08:08:08'
            }
        ]

        const arr = JSON.parse(localStorage.getItem('data')) || []
        const tbody = document.querySelector('tbody')
        const span = document.querySelector('.title span')

        // 渲染页面
        function render() {
            const newArray = arr.map(function (element, index) {
                return `
                <tr>
                    <td>${element.stuId}</td>
                    <td>${element.uname}</td>
                    <td>${element.age}</td>
                    <td>${element.gender}</td>
                    <td>${element.salary}</td>
                    <td>${element.city}</td>
                    <td>${element.time}</td>
                    <td>
                        <a href="javascript:" data-id="${index}">删除</a>
                    </td>
                </tr>
                `
            })
            tbody.innerHTML = newArray.join('')
            span.innerHTML = arr.length
        }
        render()

        // 新增数据
        const info = document.querySelector('.info')
        const username = document.querySelector('.uname')
        const age = document.querySelector('.age')
        const salary = document.querySelector('.salary')
        const gender = document.querySelector('.gender')
        const city = document.querySelector('.city')

        info.addEventListener('submit', function (e) {
            e.preventDefault()
            if (!username.value || !age.value || !gender.value) {
                alert('输入内容不能为空')
            }
            const student = {
                stuId: arr.length ? arr[arr.length - 1].stuId + 1 : 1,
                uname: username.value,
                age: age.value,
                gender: gender.value,
                salary: salary.value,
                city: city.value,
                time: new Date().toLocaleString()
            }
            console.log(student);
            arr.push(student)
            localStorage.setItem('data', JSON.stringify(arr))
            this.reset()
            render()
        })

        // 删除
        tbody.addEventListener('click', function (e) {
            if (e.target.tagName === 'A') {
                if (confirm('你确认要删除这条数据吗？')) {
                    arr.splice(e.target.dataset.id, 1)
                    localStorage.setItem('data', JSON.stringify(arr))
                }
            }
            render()
        })
    </script>
</body>

</html>